//
//  WHTdsModel.m
//  WHEasyBizLink
//
//  Created by hyf on 2024/12/6.
//

#import "WHTdsModel.h"
#import "WHWproxyKitUtility.h"
#import "NSObject+MJKeyValue.h"
#import "CommonTools.h"

@implementation WHTdsModel

/**查询TDS平台信息
 返回已注册的TDS平台，包括系统标识和代理信息
 选择一个子平台进行业务注册
 入参描述：
     {
         "callMainTdsProc": "1",
         "callFromType": "0",
         "userid":"000000"，
         "procId":"Pkg_Query_Service5.p_query_sysinfo",
         "type":"1-查询根平台 2-查询子平台"，
         "id":"TDS平台标识，多个标识用，分割"，
         "operuri" : "操作人标识",
         "operunm" : "操作人姓名"
        }
 出参描述：
 {
    "dataListAllCount": 2,
    "dataList": [
     {
        "rn": "1",
       "id": "TDS平台标识",
       "name": "TDS平台名称",
       "proxys": [
         {
           "id": "代理标识",
           "url": "代理地址"
         },
         {
           "id": "代理标识",
           "url": "代理地址"
         }
       ]
     }
       ]
     }
   ],
    "code": "EC00",
   "msg": "处理成功"
 }*/
+(void)getTDSInfoWithBlock:(void (^)(id responseObject))succese FailureBlock:(void (^)(NSString *msg))failure{
    
    NSString *json = [NSString stringWithFormat:@"\"callMainTdsProc\":\"%@\"",@"1"];
    json = [json stringByAppendingFormat:@",\"callFromType\":\"%@\"",@"0"];
    json = [json stringByAppendingFormat:@",\"userid\":\"%@\"",@"000000"];
    json = [json stringByAppendingFormat:@",\"type\":\"%@\"",@"2"];
    json = [json stringByAppendingFormat:@",\"id\":\"%@\"",@""];
    json = [json stringByAppendingFormat:@",\"operuri\":\"%@\"",getUserQcode];
    json = [json stringByAppendingFormat:@",\"operunm\":\"%@\"",getUserName];
    json = [json stringByAppendingFormat:@",\"procId\":\"Pkg_Query_Service5.p_query_sysinfo\""];
    NSString *soapStr = [NSString stringWithFormat:@"{%@}",json];
    
    NSArray *arr = @[debugAddress,debugAddress2];
    NSUInteger randomIndex = arc4random_uniform((u_int32_t)arr.count);
    NSString * proxy  = arr[randomIndex];
    NSString * target = debugAppId;
    
    WHWproxyKitUtility *whwProxyKit =
    [[WHWproxyKitUtility new] initWithProxyIP:proxy
                                    targetUri:target
                                    getParams:soapStr];

    whwProxyKit.mParamsHaveEncoded = YES;
    whwProxyKit.mUserType = @"0";
    [whwProxyKit getSyncActionWithprogress:^(NSString * _Nonnull result) {
    } success:^(NSString * _Nonnull result,NSString * _Nonnull xml, NSString * _Nonnull path){
        NSData *jsonData = [xml dataUsingEncoding:NSUTF8StringEncoding];
        NSError *err;
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err];
        NSString*code = dict[REQUEST_RESP_CODE];
        NSString*msg  = dict[REQUEST_RESP_MSG];
        if ([code isEqualToString:REQUEST_RESP_EC00]){
            NSArray *list = dict[@"dataList"];
            if (list.count>0) {
                [[NSUserDefaults standardUserDefaults] setObject:xml forKey:mTDSChildListKey];
            }
            NSArray *models = [WHTdsModel objectArrayWithKeyValuesArray:list];
            NSString *selTds = [[NSUserDefaults standardUserDefaults] objectForKey:mSelTDSChildKey];
            for (WHTdsModel *tmp in models) {
                if ([tmp.id isEqualToString:selTds]) {
                    tmp.isSel = YES;
                } else {
                    tmp.isSel = NO;
                }
            }
            succese(models);
        }else{
            failure(msg);
        }
    } failure:^(NSString * _Nonnull result) {
        failure(result);
    }];
}

#pragma mark 获取本地tds子平台列表
+ (NSArray *)getTDSList{
    NSString *xml = [[NSUserDefaults standardUserDefaults] objectForKey:mTDSChildListKey];
    if (!(xml.length>0)) {
        return [NSArray new];
    }
    NSData *jsonData = [xml dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err];
    NSArray *list = dict[@"dataList"];
    NSArray *models = [WHTdsModel objectArrayWithKeyValuesArray:list];
    
    NSString *selTds = [[NSUserDefaults standardUserDefaults] objectForKey:mSelTDSChildKey];
    for (WHTdsModel *tmp in models) {
        if ([tmp.id isEqualToString:selTds]) {
            tmp.isSel = YES;
        } else {
            tmp.isSel = NO;
        }
    }
    return  models;
}

#pragma mark 获取默认选中的本地tds子平台代理
+ (NSDictionary *)getSelTDSProxyList{
    NSString *xml = [[NSUserDefaults standardUserDefaults] objectForKey:mTDSChildListKey];
    NSString *selTds = [[NSUserDefaults standardUserDefaults] objectForKey:mSelTDSChildKey];
    if (xml.length<=0 || selTds.length<=0) {
        return [NSDictionary new];
    }
    NSData *jsonData = [xml dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err];
    NSArray *list = dict[@"dataList"];
    NSArray *models = [WHTdsModel objectArrayWithKeyValuesArray:list];
    
    NSMutableDictionary *re = [NSMutableDictionary new];
    for (WHTdsModel *tmp in models) {
        if ([tmp.id isEqualToString:selTds]) {
            NSArray *lists = tmp.proxys;
            NSUInteger randomIndex = arc4random_uniform((u_int32_t)lists.count);
            re  = lists[randomIndex];
            re[@"TDSid"] = tmp.id;
        }
    }
    return  re;
}

@end
